// pages/pay/bookedinfo/bookedinfo.js
let common = require('../../../utils/common.js');
Page({
  data: {
    order: {}
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    if (options.detail) {
      this.setData({
        order: JSON.parse(options.detail)
      });
    }
  },
  pay: function () {
    let that = this;
    common.asynPost(common.API.pay.wechat.prepay, {
      orderId: that.data.order.id
    }, function (data) {
      if (data['wechat']) {
        var wechat = data['wechat'];
        wx.requestPayment({
          'timeStamp': wechat['timestamp'],
          'nonceStr': wechat['noncestr'],
          'package': wechat['packagestr'],
          'signType': 'MD5',
          'paySign': wechat['sign'],
          'success': function (res) {
            if (res.errMsg =='requestPayment:ok') {
              var order = {
                type : 2,
                applyCode : that.data.order.applyCode,
                price:that.data.order.price
              };
              wx.redirectTo({
                url: '../paysuccess/paysuccess?order=' + JSON.stringify(order)
              });
            }
          },
          'fail': function (res) {
            console.log(res.errMsg);
          }          
        });
      }
    }, function (err) {
      console.log(err);
      wx.showToast({ title: '支付出现异常' });
    });
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  }
})